package ahu.tong.news.data;

import android.content.Context;

import androidx.annotation.Nullable;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class LocalNewsReps {
    private static final String JSON_FILE = "text.json";

    /**
     * 获取本地新闻数据源 {Json格式}
     *
     * @param context 上下文
     * @return Json格式的数据源（获取失败null）
     */
    @Nullable
    public static String getRawNewsData(Context context) {
        String result = null;
        try (final BufferedInputStream is = new BufferedInputStream(context.getAssets().open(JSON_FILE))) {
            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[512];
            int length;
            while ((length = is.read(bytes)) != -1) {
                bos.write(bytes, 0, length);
            }
            result = bos.toString();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
